package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type ActiveController struct{}

type RedPackReceiveParam struct {
}

// /红包雨配置列表
func (that *ActiveController) RedPackList(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetRedPackCfgList(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that *ActiveController) RedPackUpdate(ctx *fasthttp.RequestCtx) {
	param := model.RedPackCfgItem{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("添加和修改红包雨配置数据", ctx)
	err = model.UpdateRedPackConfig(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")

}

func (that *ActiveController) RedPackDelete(ctx *fasthttp.RequestCtx) {
	param := model.RedPackCfgItem{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("删除红包雨配置", ctx)
	err = model.UpdateRedPackConfig(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

// /红包雨领取记录
func (that *ActiveController) RedPackReceive(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetRedPackReceiveList(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// /pdd转盘未审核
func (that *ActiveController) TurnTableNoReview(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurnTableReviewList(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// /pdd转盘已领取
func (that *ActiveController) TurnTableHasReview(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurnTableReviewList(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// pdd转盘玩家明细
func (that *ActiveController) TurnTablePlayerInfo(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurnTablePlayerInfo(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that *ActiveController) TurnTableDetail(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurnTableHistoryList(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that *ActiveController) TurnTableMobileList(ctx *fasthttp.RequestCtx) {
	param := model.RedPackConfigParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurnTableMobileList(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that *ActiveController) TurnTablecheck(ctx *fasthttp.RequestCtx) {
	id := ctx.QueryArgs().GetUintOrZero("id")
	state := ctx.QueryArgs().GetUintOrZero("state")
	if id == 0 || state == 0 {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	admin := session.AdminGet(ctx)
	err := model.TurnTablecheck(id, state, admin["id"], admin["name"])
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	model.InsertLog("转盘审核操作", ctx)
	helper.RetData(ctx, true, "success")
}

func (that *ActiveController) TurnTableSetExtramt(ctx *fasthttp.RequestCtx) {
	uid := ctx.QueryArgs().GetUintOrZero("uid")
	amount := ctx.QueryArgs().GetUfloatOrZero("amount")

	if uid == 0 || amount <= 0 {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	err := model.TurnTableSetExtramt(uid, amount)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "success")

}
func (that *ActiveController) TurnTableSetTimes(ctx *fasthttp.RequestCtx) {
	uid := ctx.QueryArgs().GetUintOrZero("uid")
	times := ctx.QueryArgs().GetUintOrZero("times")

	if uid == 0 || times <= 0 {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	err := model.TurnTableSetTimes(uid, times)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "success")
}

// 批量添加号码
func (that *ActiveController) TurnTableMobileAdd(ctx *fasthttp.RequestCtx) {
	mobile := model.RurnTableMobileItem{}
	err := json.Unmarshal(ctx.PostBody(), &mobile)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if mobile.Phone == "" {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	err = model.TurnTablePhoneAdd(mobile.Phone)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	model.InsertLog("转盘添加手机分享号", ctx)
	helper.RetData(ctx, true, "success")
}

func (that *ActiveController) TurnTableMobileDelete(ctx *fasthttp.RequestCtx) {
	ids := model.RurnTableMobileItem{}
	err := json.Unmarshal(ctx.PostBody(), &ids)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if ids.Id == "" {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	err = model.TurnTablePhoneDelete(ids.Id)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	model.InsertLog("转盘删除手机分享号", ctx)
	helper.RetData(ctx, true, "success")
}

func (that *ActiveController) TurnTableMobileClear(ctx *fasthttp.RequestCtx) {

	err := model.TurnTablePhoneClear()

	if err != nil {
		helper.RetData(ctx, false, err)
		return
	}
	model.InsertLog("转盘表整表清除", ctx)
	helper.RetData(ctx, true, "success")
}

func (that ActiveController) TurntableConfig(ctx *fasthttp.RequestCtx) {
	param := model.TblTurntableConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetTurntableConfig(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that ActiveController) UpdateTurntableConfig(ctx *fasthttp.RequestCtx) {
	param := model.TblTurntableConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("转盘配置更新", ctx)
	err = model.UpdateTurntableConfig(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that ActiveController) DeleteTurntableConfig(ctx *fasthttp.RequestCtx) {
	param := model.TblTurntableConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("转盘配置删除", ctx)
	err = model.UpdateTurntableConfig(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (this *ActiveController) ComiList(ctx *fasthttp.RequestCtx) {
	param := model.TurntableCommiParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.TurntableComiList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, s)
}
